﻿using System.Collections.Generic;
using System.Linq;
using Xenta.Api;
using Xenta.Entities;
using Xenta.Enums;
using Xenta.Security;
using Xenta.Utils;

namespace Xenta.Operations
{
    #region __General__

    /// <summary>
    /// Creates a forum rank.
    /// </summary>
    public class CreateForumRank : CreateAuditable<ForumRankEntity>,
        ISecuredOperation, IApiOperation
    {
        #region Properties

        /// <summary>
        /// Gets the collection of security policies.
        /// </summary>
        public IEnumerable<SecurityPolicy> SecurityPolicies
        {
            get
            {
                yield return new SecurityPolicy()
                    .IsAuthenticated()
                    .HasPermission("FORUM");
            }
        }

        /// <summary>
        /// Gets or sets the threshold.
        /// </summary>
        public int Threshold
        {
            get;
            set;
        }

        /// <summary>
        /// Gets or sets the flags.
        /// </summary>
        public ForumRankFlags Flags
        {
            get;
            set;
        }

        #endregion

        #region Utilities

        protected override Err Validate(IUnitOfWork uow)
        {
            if(Threshold < 0)
                return Err.Of<ForumRankEntity>().NotValid("Threshold");
            return base.Validate(uow);
        }

        protected override void Construct(IUnitOfWork uow, ForumRankEntity entity)
        {
            base.Construct(uow, entity);

            entity.Threshold = Threshold;
            entity.Flags = Flags;
        }

        #endregion
    }

    /// <summary>
    /// Updates the forum rank.
    /// </summary>
    public class UpdateForumRank : UpdateAuditable<ForumRankEntity>,
        ISecuredOperation, IApiOperation
    {
        #region Properties

        /// <summary>
        /// Gets the collection of security policies.
        /// </summary>
        public IEnumerable<SecurityPolicy> SecurityPolicies
        {
            get
            {
                yield return new SecurityPolicy()
                    .IsAuthenticated()
                    .HasPermission("FORUM");
            }
        }

        /// <summary>
        /// Gets or sets the threshold.
        /// </summary>
        public int Threshold
        {
            get;
            set;
        }

        /// <summary>
        /// Gets or sets the flags.
        /// </summary>
        public ForumRankFlags Flags
        {
            get;
            set;
        }

        #endregion

        #region Utilities

        protected override Err Validate(IUnitOfWork uow)
        {
            if(Threshold < 0)
                return Err.Of<ForumRankEntity>().NotValid("Threshold");
            return base.Validate(uow);
        }

        protected override void Revise(IUnitOfWork uow, ForumRankEntity entity)
        {
            base.Revise(uow, entity);

            entity.Threshold = Threshold;
            entity.Flags = Flags;
        }

        #endregion
    }

    /// <summary>
    /// Deletes the forum rank.
    /// </summary>
    public class DeleteForumRank : DeleteEntity<ForumRankEntity>,
        ISecuredOperation, IApiOperation
    {
        #region Properties

        /// <summary>
        /// Gets the collection of security policies.
        /// </summary>
        public IEnumerable<SecurityPolicy> SecurityPolicies
        {
            get
            {
                yield return new SecurityPolicy()
                    .IsAuthenticated()
                    .HasPermission("FORUM");
            }
        }

        #endregion
    }

    /// <summary>
    /// Gets the forum rank by identifier.
    /// </summary>
    public class GetForumRank : GetEntity<ForumRankEntity>,
        IApiOperation
    {
    }

    #endregion

    #region __Translation__

    /// <summary>
    /// Creates a rank translation.
    /// </summary>
    public class CreateForumRankTranslation : CreateTranslation
        <TForumRankEntity, ForumRankEntity>,
        ISecuredOperation, IApiOperation
    {
        #region Properties

        /// <summary>
        /// Gets the collection of security policies.
        /// </summary>
        public IEnumerable<SecurityPolicy> SecurityPolicies
        {
            get
            {
                yield return new SecurityPolicy()
                    .IsAuthenticated()
                    .HasPermission("FORUM");
            }
        }

        /// <summary>
        /// Gets or sets the name.
        /// </summary>
        public string Name
        {
            get;
            set;
        }

        #endregion

        #region Utilities

        protected override void Construct(IUnitOfWork uow, TForumRankEntity entity)
        {
            base.Construct(uow, entity);

            entity.Name = StringHelper.EnsureNotNull(Name).TrimToLen(100);
        }

        #endregion
    }

    /// <summary>
    /// Updates the rank translation.
    /// </summary>
    public class UpdateForumRankTranslation : UpdateTranslation
        <TForumRankEntity, ForumRankEntity>,
        ISecuredOperation, IApiOperation
    {
        #region Properties

        /// <summary>
        /// Gets the collection of security policies.
        /// </summary>
        public IEnumerable<SecurityPolicy> SecurityPolicies
        {
            get
            {
                yield return new SecurityPolicy()
                    .IsAuthenticated()
                    .HasPermission("FORUM");
            }
        }

        /// <summary>
        /// Gets or sets the name.
        /// </summary>
        public string Name
        {
            get;
            set;
        }

        #endregion

        #region Utilities

        protected override void Revise(IUnitOfWork uow, TForumRankEntity entity)
        {
            base.Revise(uow, entity);

            entity.Name = StringHelper.EnsureNotNull(Name).TrimToLen(100);
        }

        #endregion
    }

    /// <summary>
    /// Saves a rank translation.
    /// </summary>
    public class SaveForumRankTranslation : SaveTranslation
        <TForumRankEntity, ForumRankEntity>,
        ISecuredOperation, IApiOperation
    {
        #region Properties

        /// <summary>
        /// Gets the collection of security policies.
        /// </summary>
        public IEnumerable<SecurityPolicy> SecurityPolicies
        {
            get
            {
                yield return new SecurityPolicy()
                    .IsAuthenticated()
                    .HasPermission("FORUM");
            }
        }

        /// <summary>
        /// Gets or sets the name.
        /// </summary>
        public string Name
        {
            get;
            set;
        }

        #endregion

        #region Utilities

        protected override void Work(IUnitOfWork uow, out TForumRankEntity result)
        {
            var translation = uow.Execute(new GetForumRankTranslationByLanguage
            {
                HolderID = HolderID,
                LanguageID = LanguageID
            }).Result;
            if(translation == null)
            {
                translation = uow.Execute(new CreateForumRankTranslation
                {
                    HolderID = HolderID,
                    LanguageID = LanguageID,
                    Name = Name
                }).Result;
            }
            else
            {
                translation = uow.Execute(new UpdateForumRankTranslation
                {
                    EntityID = translation.EntityID,
                    LanguageID = LanguageID,
                    Name = Name
                }).Result;
            }
            result = translation;
        }

        #endregion
    }

    /// <summary>
    /// Deletes the rank translation.
    /// </summary>
    public class DeleteForumRankTranslation : DeleteTranlsation
        <TForumRankEntity, ForumRankEntity>,
        ISecuredOperation, IApiOperation
    {
        #region Properties

        /// <summary>
        /// Gets the collection of security policies.
        /// </summary>
        public IEnumerable<SecurityPolicy> SecurityPolicies
        {
            get
            {
                yield return new SecurityPolicy()
                    .IsAuthenticated()
                    .HasPermission("FORUM");
            }
        }

        #endregion
    }

    /// <summary>
    /// Gets the forum rank translation.
    /// </summary>
    public class GetForumRankTranslation : GetTranlsation
        <TForumRankEntity, ForumRankEntity>,
        IApiOperation
    {
    }

    /// <summary>
    /// Gets the forum rank translation.
    /// </summary>
    public class GetForumRankTranslationByLanguage : GetTranlsationByLanguage
        <TForumRankEntity, ForumRankEntity>,
        IApiOperation
    {
    }

    #endregion

    #region __Search__

    /// <summary>
    /// Search over forum rank collection.
    /// </summary>
    public class SearchForumRanks : SearchOperation<ForumRankEntity>,
        IApiOperation
    {
        #region Ctors

        /// <summary>
        /// Initializes a new class instance.
        /// </summary>
        public SearchForumRanks()
        {
            SortDir = SortDirection.Ascending;
            SortBy = new []
            {
                ForumRankSortBy.ID
            };
        }

        #endregion

        #region Properties

        /// <summary>
        /// Gets or sets the flag mask.
        /// </summary>
        public ForumRankFlags FlagMask
        {
            get;
            set;
        }

        /// <summary>
        /// Gets or sets the sort direction.
        /// </summary>
        public SortDirection SortDir
        {
            get;
            set;
        }

        /// <summary>
        /// Gets or sets the sort by.
        /// </summary>
        public ForumRankSortBy[] SortBy
        {
            get;
            set;
        }

        #endregion

        #region Utilities

        protected override IQueryable<ForumRankEntity> Build(IUnitOfWork uow, IQueryable<ForumRankEntity> query)
        {
            query = base.Build(uow, query);
            query = query.FilterByFlags(x => (int)x.Flags, (int)FlagMask);
            if(SortBy != null)
            {
                for(int i = 0; i < SortBy.Length; i++)
                {
                    switch(SortBy[i])
                    {
                        case ForumRankSortBy.Threshold:
                            query = query.OrderBy(x => x.Threshold, SortDir, i != 0);
                            break;
                        case ForumRankSortBy.CreatedOn:
                            query = query.OrderBy(x => x.CreatedOn, SortDir, i != 0);
                            break;
                        default:
                            query = query.OrderBy(x => x.EntityID, SortDir, i != 0);
                            break;
                    }
                }
            }
            return query;
        }

        #endregion
    }

    #endregion
}
